#!/bin/bash
# 加载系统环境变量
source /etc/profile
# 脚本所在目录
basedir=$(cd $(dirname $0);pwd)
# jobmanager list
JOBMANAGER_LIST=$(cat ${basedir}/conf/masters | cut -d ":" -f 1)
# taskmanager list
TASKMANAGER_LIST=$(cat ${basedir}/conf/workers)
# flink安装目录
FLINK_DIR="/usr/local/flink-1.17.1"

add_crontab () {
    echo -e "==========\033[32m jobmanager add\033[0m =========="
    for job_ip in ${JOBMANAGER_LIST[@]}
    do
        ssh root@${job_ip} "echo \"*/10 * * * * ${FLINK_DIR}/check_status.sh jobmanager\" >> /var/spool/cron/root"
        if [[ $? = '0' ]];then
          echo "${job_ip} add crontab task success for check jobmanager status!"
        else
          echo "${job_ip} add crontab task unsuccess for check jobmanager status!"
        fi
    done
    echo -e "==========\033[32m taskmanager add\033[0m =========="
    for task_ip in ${TASKMANAGER_LIST[@]}
    do
        ssh root@${task_ip} "echo \"*/2 * * * * ${FLINK_DIR}/check_status.sh taskmanager\" >> /var/spool/cron/root"
        if [[ $? = '0' ]];then
          echo "${task_ip} add crontab task success for check taskmanager status!"
        else
          echo "${task_ip} add crontab task unsuccess for check taskmanager status!"
        fi
    done
}

delete_crontab () {
    echo -e "==========\033[31m jobmanager delete\033[0m =========="
    for job_ip in ${JOBMANAGER_LIST[@]}
    do
        JOB_CRON_ONLY_NUM=$(ssh root@${job_ip} "grep -n \"${FLINK_DIR}\" /var/spool/cron/root | grep 'jobmanager'" | wc -l)
        if [[ ${JOB_CRON_ONLY_NUM} = '1' ]];then
            JOB_CRON_NUM=$(ssh root@${job_ip} "grep -n \"${FLINK_DIR}\" /var/spool/cron/root | grep 'jobmanager'" | cut -d ':' -f 1)
            ssh root@${job_ip} "sed -i \"${JOB_CRON_NUM}d\" /var/spool/cron/root"
            if [[ $? = '0' ]];then
              echo "${job_ip} delete crontab task success for check jobmanager status!"
            else
              echo "${job_ip} delete crontab task unsuccess for check jobmanager status!"
            fi
        else
            echo "${job_ip} crontab task is not only for jobmanager status OR not crontab, please manual check!"
        fi
    done
    echo -e "==========\033[31m taskmanager delete\033[0m =========="
    for task_ip in ${TASKMANAGER_LIST[@]}
    do
        TASK_CRON_ONLY_NUM=$(ssh root@${task_ip} "grep -n \"${FLINK_DIR}\" /var/spool/cron/root | grep 'taskmanager'" | wc -l)
        if [[ ${TASK_CRON_ONLY_NUM} = '1' ]];then
            TASK_CRON_NUM=$(ssh root@${task_ip} "grep -n \"${FLINK_DIR}\" /var/spool/cron/root | grep 'taskmanager'" | cut -d ':' -f 1)
            ssh root@${task_ip} "sed -i \"${TASK_CRON_NUM}d\" /var/spool/cron/root"
            if [[ $? = '0' ]];then
              echo "${task_ip} delete crontab task success for check taskmanager status!"
            else
              echo "${task_ip} delete crontab task unsuccess for check taskmanager status!"
            fi
        else
            echo "${task_ip} crontab task is not only for taskmanager status OR not crontab, please manual check!"
        fi
    done
}
check_crontab () {
    echo -e "==========\033[36m jobmanager check\033[0m =========="
    for job_ip in ${JOBMANAGER_LIST[@]}
    do
        echo "${job_ip} crontab task for jobmanager:"
        ssh root@${job_ip} "grep -n \"${FLINK_DIR}\" /var/spool/cron/root | grep 'jobmanager'"
    done
    echo -e "==========\033[36m taskmanager check\033[0m =========="
    for task_ip in ${TASKMANAGER_LIST[@]}
    do
        echo "${task_ip} crontab task for taskmanager:"
        ssh root@${task_ip} "grep -n \"${FLINK_DIR}\" /var/spool/cron/root | grep 'taskmanager'"
    done
}
case $1 in
  add)
    add_crontab
  ;;
  delete)
    delete_crontab
  ;;
  check)
    check_crontab
  ;;
  *)
    echo "pleace run: $0 check|add|delete"
  ;;
esac

